import os
import sys
localDir = os.path.dirname(__file__)
curpath = os.path.normpath(os.path.join(os.getcwd(), localDir))
grandparent = os.path.normpath(os.path.join(curpath, '../'))
sys.path.insert(0, grandparent)

import unittest

from lib.rename_table import RenameTable

from databases.mysql import Mysql
from databases.sqlite import Sqlite
from databases.db2 import Db2
from databases.mssql import Mssql
from databases.oracle import Oracle
from databases.postgres import Postgres

config = {"development" : {"db" : "sqlite:///:memory:"}}

class TestRenameTable(unittest.TestCase):

    def testTruth(self):
        self.assertTrue(True)

    def testInitialiazeMysql(self):
        alc = Mysql(config['development']['db'])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_infos", sql.new_name)
        self.assertEqual("ALTER TABLE schema_info RENAME TO schema_infos;", sql.query)

    def testInitialiazeSqlite(self):
        alc = Sqlite(config['development']['db'])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_infos", sql.new_name)
        self.assertEqual("ALTER TABLE schema_info RENAME TO schema_infos;", sql.query)

    def testInitialiazeDb2(self):
        alc = Db2(config['development']['db'])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_infos", sql.new_name)
        self.assertEqual("RENAME TABLE schema_info TO schema_infos;", sql.query)

    def testInitialiazePostgres(self):
        alc = Postgres(config['development']['db'])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_infos", sql.new_name)
        self.assertEqual("ALTER TABLE schema_info RENAME TO schema_infos;", sql.query)

    def testInitialiazeMssql(self):
        alc = Mssql(config['development']['db'])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_infos", sql.new_name)
        self.assertEqual(None, sql.query)

    def testInitialiazeOracle(self):
        alc = Oracle(config['development']['db'])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)

        self.assertEqual("schema_info", sql.table)
        self.assertEqual("schema_infos", sql.new_name)
        self.assertEqual("ALTER TABLE schema_info RENAME TO schema_infos;", sql.query)

    def testExecute(self):
        alc = Sqlite(config['development']['db'])
        alc._create_schema()
        result = alc.execute_raw_query("SELECT * FROM schema_info").fetchone()
        self.assertEquals('000', result[0])

        query = "schema_info, schema_infos"
        sql = RenameTable(alc, query)
        self.assertTrue(sql.execute())

        result = alc.execute_raw_query("SELECT * FROM schema_infos").fetchone()
        self.assertEquals('000', result[0])
        
        try:
            print alc.execute_raw_query("SELECT * FROM schema_info").fetchall()
            self.fail("Should not have found the schema_info table")
        except:
            # Move along people nothing to see here
            pass


if __name__ == "__main__":
    unittest.main()
